package org.gdgbologna.devfest;

import java.lang.reflect.Field;

import org.gdgbologna.devfest.adapter.SponsorAdapter;
import org.gdgbologna.devfest.adapter.TalkAdapter;
import org.gdgbologna.resources.Devfest;
import org.gdgbologna.resources.Session;
import org.gdgbologna.resources.Talk;

import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

public class SponsorListFragment extends Fragment {
	
	public static String NAME_DEVFEST = "name";
	
	String[]		sponsor;
	ApplicationDevFest app;
	MainActivity	act;
	ListView 		list;
	String 			name;
	
	private static SponsorAdapter	adapter;
	
	public static SponsorListFragment newInstance(String name){
		SponsorListFragment f = new SponsorListFragment();
		
		Bundle args = new Bundle();
		args.putString("fest", name);
		f.setArguments(args);
		
		return f;
	}
	
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View V = inflater.inflate(R.layout.sponsor_fragment, container, false);

        list = (ListView) V.findViewById(R.id.list);
        
        act = (MainActivity) getActivity();
        app = (ApplicationDevFest) act.getApplicationContext();
 
        
        name = getArguments().getString("fest");
        if (savedInstanceState != null) {
        	name = savedInstanceState.getString("fest");
        }
        
		loadDevfest();
        
        return V;
    }

	public void loadDevfest(){
		
		Class raw = R.array.class;
        Field field = null;
		try {
			field = raw.getField("sponsor_"+name);
			sponsor = app.getResources().getStringArray(field.getInt(null));
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		adapter = new SponsorAdapter(app,sponsor);
        list.setAdapter(adapter);
	}
	
	@Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("fest", name);
    }


	public void changeDevfest(String _fest) {
		name = _fest;
		loadDevfest();
	}
	
}
